package com.star.controller.business;

import com.star.result.Result;
import com.star.service.ShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;


@RestController("adminShopController")
@RequestMapping("/business/shop")
@Slf4j
@Api(tags = "商家端店铺状态相关接口")
public class ShopController {
    public static final String KEY = "SHOP_STATUS";
    @Autowired
    private RedisTemplate<String, Integer> redisTemplate;
    @Autowired
    private ShopService shopService;

    /**
     * 设置店铺营业状态
     * @param status
     * @return
     */
    @PutMapping("/{status}")
    @ApiOperation("设置店铺营业状态")
    public Result<String> setStatus(@PathVariable Integer status){
        //店铺营业状态：1为营业，0为打烊
        log.info("设置店铺营业状态{}", status == 1 ? "营业" : "打烊");
        shopService.setStatus(status);
        redisTemplate.opsForValue().set(KEY, status);
        return Result.success();
    }

    /**
     * 获取店铺营业状态
     * @return
     */
    @GetMapping("/status")
    @ApiOperation("获取店铺营业状态")
    public Result<Integer> getStatus(){
        Integer status = redisTemplate.opsForValue().get(KEY);
        log.info("获取店铺营业状态{}", status == 1 ? "营业" : "打烊");
        return Result.success(status);
    }
}
